package app.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import database.local.Business;
import database.local.Deal;
import database.local.OverlayItemDrawable;
import database.local.Utils;

public class DrinkItemizedOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItemDrawable> mOverlays = new ArrayList<OverlayItemDrawable>();
	private Context mContext;
	private List<Business> businessList;
	private Map<String, View> viewsMap = new HashMap<String, View>();
	private MapView mapView;
	private int selectedItem;
	private DetailPopupLayoutData dpld;
	
	public DrinkItemizedOverlay(Drawable defaultMarker) {
		
		super(boundCenterBottom(defaultMarker)); //Define the pin of the item to be in the center button.
		// TODO Auto-generated constructor stub
	}
	
	//Constructor to get the context of the view (allowing touch actions)
	public DrinkItemizedOverlay(Drawable defaultMarker, Context context, List<Business> bList,MapView mView,
			DetailPopupLayoutData dpld) {
		  super(boundCenterBottom(defaultMarker));
		  this.mContext = context;
		  this.businessList = bList;
		  this.mapView = mView;
		  this.dpld = dpld;
		 }
	
	
	public void addOverlay(OverlayItemDrawable overlay) {
	    mOverlays.add(overlay);
	    populate();
	}

	/**
	 * 
	 * Both functions createItem and size are required for the populate method of addOverlay 
	 */
	
	@Override
	protected OverlayItem createItem(int arg0) {
		return mOverlays.get(arg0);
		
	}
	
	@Override
	public int size() {
		return mOverlays.size();
	}
	
	
	//Handle events when tapped by the user (need to change it)
	protected boolean onTap(int index) {
		//get the selected item
		OverlayItem item = mOverlays.get(index);
		Drawable icon = mContext.getResources().getDrawable(R.drawable.drink1_system_icon_selected);
		setItemSelected(index);
		
		//use the item title to get the business from the business list.
		final Business b = getBusinessFromList(item.getTitle());
		
		//get the first deal from the deals' list
		if (b != null && b.getDeals() != null) {
			final Deal d = b.getDeals().get(0);
		
		//use the deal to fill the popupWindow   	  	
    	fillDetailsForBusiness(d, b,  dpld);
    	
    	
		}
	  return true;
	}


	protected void openDealsList(Business b) {
		Intent openDealListsActivity = new Intent(mContext, DealsListView.class);
		Bundle bundle = new Bundle();
		
		bundle.putParcelable("business", b);
		
		openDealListsActivity.putExtra("values", bundle);
		mContext.startActivity(openDealListsActivity);		
	}

	protected void openDealWindow(Business b, Deal d) {
		Intent openDealActivity = new Intent(mContext, DealViewActivity.class);
		Bundle bundle = new Bundle();
		
		bundle.putParcelable("deal", d);
		bundle.putParcelable("business", b);
		
		openDealActivity.putExtra("values", bundle);
		mContext.startActivity(openDealActivity);
		}

	private void fillDetailsForBusiness(Deal d,Business b, DetailPopupLayoutData dpld){
		dpld.setDNameText(d.getShortName());
		dpld.setBNameText(b.getName());
		dpld.setAddressText(b.getFullAddress());
		dpld.setRate(d.getRate());
		dpld.setMoney(d.getCostLevel());
		dpld.setDistanceText(Utils.getDistanceStr(b.getDistance()));
		
		dpld.setImage(d, mContext );
		final Business fb = b;
		final Deal fd = d;
		dpld.getLayout().setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				openDealWindow(fb, fd);
			}
			});
		
		dpld.setVisibilty(true);
	}
	
	private Business getBusinessFromList(String name){
		for (int i = 0; i <= businessList.size() -1 ; i++){
			if (businessList.get(i).getName().equalsIgnoreCase(name))
				return businessList.get(i);
		}
		return null;
	}
	
	public void removeAllOverlays(){
		mOverlays = new ArrayList<OverlayItemDrawable>();
	}
		public void setBusinessList(List<Business> list){
			businessList = list;
		}
		
	public void	setAllIconsToDefault(){
			for (OverlayItemDrawable item : mOverlays){
				item.setMarker(item.getDefaultIcon());
			}
			selectedItem = -1;
		}

	
	public void defaultSelectedItem(){
		if (selectedItem  >= 0  && selectedItem <= (mOverlays.size() -1) ){
		OverlayItemDrawable oid = mOverlays.get(selectedItem);
		oid.setMarker(oid.getDefaultIcon());
		selectedItem = -1;
		}
		
	}
	public void setItemSelected (int item){
		
		//unselect previous selected item
		defaultSelectedItem();
		
		//if item required is in range select it.
		if (item  >= 0  && item <= (mOverlays.size() -1) ){
		OverlayItemDrawable oid = mOverlays.get(item);
		oid.setMarker(oid.getSelectedIcon());
		selectedItem = item;
		}
		
	}

	public void selectBusiness(Business b) {
		OverlayItem curItem = null;
		int index = 0;
		for (OverlayItem item:mOverlays){
			if (item.getTitle().equalsIgnoreCase(b.getName())){
				curItem = item;
				break;
			}
			index++;
		}
		if (curItem != null){
			Log.w("DrinkItimez","Index = " + index + "Item = " + curItem.getTitle());
			setItemSelected(index);
			//get the first deal from the deals' list
			if (b != null && b.getDeals() != null) {
				final Deal d = b.getDeals().get(0);			
			//use the deal to fill the popupWindow   	  	
	    	fillDetailsForBusiness(d, b,  dpld);
			
		    }
		}
	}
}


